package com.sinosoft.hydra.um.shiro;

import com.sinosoft.hydra.um.domain.permission.SysUserMember;
import com.sinosoft.hydra.um.services.SysResourceService;
import com.sinosoft.hydra.um.services.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

;

/**
 * shiro 登录及授权拦截器
 *
 * @author Zhengt@sinux.com.cn
 * @time 2015-2-1
 * @desc shiro 登录及授权拦截器
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 授权
     */

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
        String loginId = (String)arg0.getPrimaryPrincipal();
        SimpleAuthorizationInfo sazi = new SimpleAuthorizationInfo();
        Set<String> roleNames = new HashSet<String>();
        Set<String> result = sysResourceService.findUserPermissions(loginId, roleNames);
        sazi.addRoles(roleNames);
        sazi.addStringPermissions(result);
        return sazi;
    }

    /**
     * 认证
     */

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) {
        if (arg0 instanceof CaToken) {
            return doGetAuthenticationInfoByCa((CaToken) arg0);
        } else if (arg0 instanceof UsernamePasswordToken) {
            return doGetAuthenticationInfoByDataBase((UsernamePasswordToken) arg0);
        } else {
            throw new AuthenticationException();
        }
    }

    private AuthenticationInfo doGetAuthenticationInfoByDataBase(UsernamePasswordToken upToken) {
        String password = String.valueOf(upToken.getPassword());
        SysUserMember us = sysUserService.doGetAuthenticationInfoByNumber(upToken.getUsername());
        if (us == null || !us.getUserPassword().equals(password) || us.getUserNumber() == null || !us.getUserNumber().equals(upToken.getUsername())) {
            throw new AuthenticationException();
        }

        return new SimpleAuthenticationInfo(us.getId(), password.toCharArray(), getName());
    }

    private AuthenticationInfo doGetAuthenticationInfoByCa(CaToken caToken) {
        return null;
    }

    /**
     * 清楚授权信息
     */

    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    /**
     * 清楚认证信息
     */

    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    /**
     * 清楚登陆缓存
     */

    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    /**
     * 清楚所有授权信息
     */
    public void clearAllCachedAuthorizationInfo() {
        if (getAuthorizationCache() != null) {
            getAuthorizationCache().clear();

        }
    }

    /**
     * 清楚所有认证信息
     */
    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    /**
     * 清楚所有缓存
     */
    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}  
